<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/25
 * Time: 14:03
 */

namespace app\controllers;

use yii;
use yii\db\Query;

class RoleController extends ApiBaseController
{
    public function actionRules()
    {
        $db = new Query();
        $list = $db->select('*')->from('jingsourcing_adminrole')
            ->all();
        return \json_encode(['list' => $list]);
    }

    public function actionUpdate()
    {
        $data = yii::$app->request->post();
        foreach ($data as $k => $v) {
            if (!in_array($k, ['id', 'rules'])) {
                return json_encode(['code' => 0, 'msg' => '参数错误']);
            }
        }

        $re = yii::$app->db->createCommand()
            ->update('jingsourcing_adminrole', ['new_rules' => $data['rules']], ['id' => $data['id']])
            ->execute();
        if ($re) {
            return \json_encode(['code' => 1, 'msg' => '更新成功']);
        } else {
            return \json_encode(['code' => 0, 'msg' => '更新失败']);
        }
    }
}